using Sedna.DAO;
using Sedna.DomainService;
using Sedna.Core.Persistence;
using Sedna.Core.Domain;

namespace Sedna.DomainService
{
    public class ApplicationParameterService : HbmBaseDomainService, IApplicationParameterService
    {
        private IBaseDAO baseDAO;

        public IBaseDAO BaseDAO
        {
            set { baseDAO = value; }
        }

        [Transactional]
        public ApplicationParameter GetParameterByName(string name)
        {
            ApplicationParameter result = baseDAO.GetById(typeof(ApplicationParameter),
                name) as ApplicationParameter; 
            return result;
        }

        [Transactional] 
        public string GetParameterValue(string name)
        {
            ApplicationParameter param = GetParameterByName(name);
            return param!=null?param.Value:null;
        }
        
        [Transactional]
        public void SetParameterValue(string name, string value)
        {
            ApplicationParameter param = GetParameterByName(name);
            if (param == null)
            {
                param = new ApplicationParameter();
                param.Name = name;
            }
            param.Value = value;
            baseDAO.SaveOrUpdate(param);
        }
        
    }
}
